package com.tomas.demo.common.utils;

import java.util.UUID;

/**
 * @author Tomas
 * @date 16/7/11 下午12:20
 * @what
 */
public class FileUtils {

    public static String GetUid()
    {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }

    /**
     * 获取文件后缀名
     * @param filename
     * @return
     */
    public static String getFileExt(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return "";
    }

    /**
     * 替换文件后缀名
     * @param filename
     * @param newExt
     * @return
     */
    public static String replaceFileExt(String filename, String newExt) {
        if ((filename != null) && (filename.length() > 0))
        {
            int dot = filename.lastIndexOf('.');
            if(dot > -1){
                if(dot < (filename.length() - 1)){
                    return filename.substring(0, dot+1) + newExt;
                }else{
                    return filename + newExt;
                }
            }else{
                return filename + "." + newExt;
            }
        }
        return filename;
    }
}
